﻿using Bestcode.MathParser;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using iTextSharp.text;
using iTextSharp.text.pdf;
using System.IO;

namespace Tubes4_Metnum.Machine
{
    public class TitikTengah
    {
        MathParser parser;
        public double[] dataX;
        public double[] dataY;
        public double a;
        public double b;
        public double h;
        public int n;
        public double result;

        public TitikTengah()
        {
            parser = new MathParser();
        }

        public void setDataX(double[] data)
        {
            this.dataX = data;
        }

        public void setDataY(double[] data)
        {
            this.dataY = data;
        }

        public void setExpression(String exp)
        {
            parser.Expression = exp;
        }

        public double evalX(double X)
        {
            parser.X = X;
            return (double)parser.Value;
        }

        public double evalXY(double X, double Y)
        {
            parser.X = X;
            parser.Y = Y;
            return (double)parser.Value;
        }

        public double integral_data(double[] dX, double[] dY)
        {
            result = 0;
            this.dataX = dX; this.dataY = dY;
            this.n = dataX.Length - 1;
            this.h = (b - a) / n;
            this.a = dataX[0]; this.b = dataX[this.n];

            for (int i = 0; i < dataX.Length; i++)
            {
                result += dataY[i];
            }
            result = h * result;

            return result;
        }

        public double integral(double a, double b, int n)
        {
            double result = 0;

            double h = (b - a) / n;
            dataX = new double[n];
            for (int i = 0; i < dataX.Length; i++)
            {
                dataX[i] = a + h * (i + 0.5);
            }

            dataY = new double[dataX.Length];
            for (int i = 0; i < dataY.Length; i++)
            {
                parser.X = dataX[i];
                dataY[i] = (double) parser.Value;
                result += dataY[i];
            }

            result = h * result;

            this.a = a; this.b = b;
            this.n = n; this.h = h;
            this.result = result;

            return result;
        }

        public void printdataX()
        {
            String output = "";
            for (int i = 0; i < dataX.Length; i++)
            {
                output += dataX[i] + "-";
            }
            output = output.Substring(0, output.Length - 1);
            Console.WriteLine(output);
        }

        public void printdataY()
        {
            String output = "";
            for (int i = 0; i < dataY.Length; i++)
            {
                output += dataY[i] + "-";
            }
            output = output.Substring(0, output.Length - 1);
            Console.WriteLine(output);
        }

        // ubah 
        public void saveFile(string filePath)
        {
            Document document = new Document();
            try
            {
                string ext = filePath.Substring(filePath.Length - 4, 4);
                if (ext.CompareTo(".pdf") != 0) filePath += ".pdf";
                PdfWriter.GetInstance(document, new FileStream(filePath, FileMode.Create));
                document.Open();

                /* untuk Trapesium */
                document.Add(new Paragraph("Metode Titik Tengah"));

                PdfPTable ptable;
                PdfPCell pcell;
                document.Add(new Paragraph("a = " + this.a + "; b = " + this.b + "; h = " + this.h + "; n = " + this.n));
                ptable = new PdfPTable(2);
                iTextSharp.text.Font arial7 = FontFactory.GetFont("ARIAL", 7);
                pcell = new PdfPCell(new Phrase(new Chunk("X", arial7)));
                ptable.AddCell(pcell);
                pcell = new PdfPCell(new Phrase(new Chunk("Y", arial7)));
                ptable.AddCell(pcell);
                for (int rows = 0; rows < this.dataX.Length; rows++)
                {
                    pcell = new PdfPCell(new Phrase(new Chunk(this.dataX[rows].ToString(), arial7)));
                    ptable.AddCell(pcell);
                    pcell = new PdfPCell(new Phrase(new Chunk(this.dataY[rows].ToString(), arial7)));
                    ptable.AddCell(pcell);
                }
                document.Add(new Paragraph("\n"));
                document.Add(ptable);
                document.Add(new Paragraph("Hasil integral: " + this.result));
            }
            catch (DocumentException de)
            {

            }
            catch (IOException ioe)
            {

            }
            document.Close();
        }

    }
}
